package Application.Layer;

import Application.AppJoint;
import Application.LayerImage.LayerImage;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;

/**
 * 图层类
 */
public class Layer {
    // 可见性
    private boolean visible = true;

    public boolean isLocked() {
        return locked;
    }

    // 是否锁定
    private boolean locked = false;
    // 不透明度
    private int alpha = 100;
    // 混合模式
    private int blendMode = 0;

    public String getName() {
        return name;
    }

    // 名字
    private String name;
    // Id
    private int id;


    public GridPane getRoot() {
        return root;
    }

    public void setRoot(GridPane root){
        this.root = root;
    }

    private GridPane root;

    static public int globalId = 0;
    // 主要画布
    private LayerImage paint;
//    // 蒙版
//    private LayerImage mask;

    public Layer(String name,int width, int height, WritableImage ... writableImages) {
        if(writableImages.length==0){
            paint = new LayerImage(width, height);
            for(int i = 0; i < width; i++) {
                for(int j = 0; j < height; j++) {
                    paint.setPixelArgb(i, j, 0x00ffffff);
                }
            }
        }else{
            paint = new LayerImage(width,height,writableImages[0]);
        }

        //mask = new LayerImage(width, height);
        this.name = name;
        this.id = globalId;
        globalId++;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

    public int getAlpha() {
        return alpha;
    }

    public void setBlendMode(int blendMode) {
        this.blendMode = blendMode;
    }

    public int getBlendMode() {
        return this.blendMode;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public Color getColor(int x, int y) {
        return paint.getPixelColor(x, y);
    }

    public int getArgb(int x, int y) {
        return paint.getPixelArgb(x, y);
    }

    public void setArgb(int x, int y, int argb) {
        paint.setPixelArgb(x, y, argb);
    }

    public void setColor(int x, int y, Color color) {
        paint.setPixelColor(x, y, color);
    }

    public boolean getVisible() {
        return this.visible;
    }

    public LayerImage getLayerImage() {
        return paint;
    }

    public LayerImage cloneLayerImage() {
        int width = AppJoint.getInstance().getCanvasWidth();
        int height = AppJoint.getInstance().getCanvasHeight();
        LayerImage ret = new LayerImage(width, height);

        for(int i = 0; i < width; i++) {
            for(int j = 0; j < height; j++) {
                int pixel = paint.getPixelArgb(i, j);
                ret.setPixelArgb(i, j, pixel);
            }
        }

        return ret;
    }
}
